package sv.com.globalsolutionslt.controlador;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author 43579648
 */
public abstract class AbstractFacade<T> implements Serializable{
    private Class<T> entityClass;
    private T entity;

    public AbstractFacade(Class<T> entityClass) {
            this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();
    
    public void crear() throws Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.persist(entity);
        }catch(Exception e){
            throw e;
        }
    }
    
    public void modificar() throws Exception {
         EntityManager em = null;
        try{
            em = getEntityManager();
            em.merge(entity);
         }catch(Exception e){
            throw e;
        }
    }

    public void remover() throws Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.remove(getEntityManager().merge(entity));
            em.flush();
        }catch(Exception e){
            throw e;
        }
    }

    public void crear(T entity) throws Exception {
         EntityManager em = null;
        try{
            em = getEntityManager();
            em.persist(entity);
        }catch(Exception e){
            throw e;
        }
    }

    public void modificar(T entity) throws Exception {
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.merge(entity);
        }catch(Exception e){
            throw e;
        }
    }
    
    
    public void guardar(T entity) throws Exception{
        modificar(entity);
    }

    public void remover(T entity) throws Exception {
         EntityManager em = null;
        try{
            em = getEntityManager();
            em.remove(em.merge(entity));
        }catch(Exception e){       
            throw e;
        }
    }

    public T encontrarPorID(Object id) {
        
        return getEntityManager().find(entityClass, id);
    }

    public List<T> obtenerTodos() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        List<T> list = getEntityManager().createQuery(cq).getResultList();
        for(T t : list){
            getEntityManager().refresh(t);
        }
        return list;
    }    
    
    public List<T> obtenerTodosActivos(){
        Query q = getEntityManager().createQuery("Select distinct o from " + entityClass.getSimpleName() + " o where o.activo = :activo");
        q.setParameter("activo", true);
        return q.getResultList();
    }
    
    public List<T> obtenerTodosDistintos() {
        Query q = getEntityManager().createQuery("Select distinct o from " + entityClass.getSimpleName() + " o");
        return q.getResultList();
    }

    public List<T> obtenerTodosRango(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int contadorTodos() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }    
}
